package com.quotes.datamodell;

import java.util.Date;
import org.apache.log4j.Logger;

public class QuotesConverter 
{
	public static DataTable toDataTable(Quotes quotes) {
		DataTable table = new DataTable("date", "open", "close", "high", "low", "volume", "adjclose");
		Date[] dates = quotes.getQuote(QuotesType.CLOSE).DatesAsArray();
		
		try {
			for (Date d : dates) {
				table.addRowFromValues(d,
						quotes.getQuote(QuotesType.OPEN).get(d),
						quotes.getQuote(QuotesType.CLOSE).get(d),
						quotes.getQuote(QuotesType.HIGH).get(d),
						quotes.getQuote(QuotesType.LOW).get(d),
						quotes.getQuote(QuotesType.VOLUME).get(d),
						quotes.getQuote(QuotesType.ADJCLOSE).get(d));
			}
		}
		catch (Exception e) {
			 Logger.getLogger("QuotesConverter").error("Could not create DataTable!", e);
		}
		
		return table;
	}
	
	public static Quotes toQuotes(DataTable table, Symbol symbol) {
		Quotes quotes = new Quotes(symbol);
		
		for (DataRow row : table.getRows()) {
			quotes.addQuote(row.getDateValue(0), 
					row.getDoubleValue(1), 
					row.getDoubleValue(2), 
					row.getDoubleValue(3), 
					row.getDoubleValue(4), 
					row.getDoubleValue(5), 
					row.getDoubleValue(6));
		}
		
		return quotes;
	}
}
